package com.canomo.modules.inventory.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.canomo.base.constant.BaseConstant;
import com.canomo.modules.inventory.entity.Product;
import com.canomo.modules.inventory.entity.ProductDetail;
import com.canomo.modules.inventory.entity.ProductSupplier;
import com.canomo.modules.inventory.service.ProductDetailService;
import com.canomo.modules.inventory.service.ProductService;
import com.canomo.modules.inventory.service.ProductSupplierService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author xiaoj
 * @since 2018-07-14
 */
@Controller
@RequestMapping("/inventory/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductDetailService productDetailService;

    @Autowired
    private ProductSupplierService productSupplierService;

    @Autowired
    private HttpServletRequest request;

    private ObjectMapper mapper = new ObjectMapper();

    @RequestMapping("")
    public ModelAndView init() {
        return new ModelAndView("/inventory/product");
    }

    /**
    * 查询列表
    * @param currentPage
    * @param pageSize
    * @return
    */
    @RequestMapping("list/{currentPage}/{pageSize}")
    @ResponseBody
    public Page<Product> findList(@PathVariable String currentPage, @PathVariable String pageSize,@RequestBody Product product) {
        Page<Product> page = new Page<>(Integer.parseInt(currentPage),Integer.parseInt(pageSize));
        //对Name去除前后空格
        product.setName(StringUtils.trim(product.getName()));
        page = productService.getProductPage(page, product);
        return page;
    }

    @RequestMapping("{id}")
    @ResponseBody
    public Product findProductById(@PathVariable("id") String id){
        return productService.findProductById(id);
    }

    /**
    * 删除单条记录
    * @param product
    * @return
    */
    @RequestMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody Product product) {
        return productService.deleteById(product.getId());
    }

    /**
    * 保存记录
    * @param product
    * @return
    */
    @RequestMapping("save")
    @ResponseBody
    public boolean save(@RequestBody Product product) {
        try {
            productService.insertOrUpdate(product);
            ProductDetail productDetail = product.getDetail();
            productDetail.setProductId(product.getId());
            productDetailService.insertOrUpdate(productDetail);
            //保存产品对应的供货商信息
            //先删除旧的信息
            Wrapper<ProductSupplier> ew = new EntityWrapper<>();
            ew.and("product_id",product.getId());
            productSupplierService.delete(ew);
            if (CollectionUtils.isNotEmpty(product.getSuppliers())) {
                productSupplierService.insertBatch(product.getSuppliers());
            }
            return true;

        } catch (Exception e) {
            return false;
        }

    }

    /**
    * 批量删除
    * @param map
    * @return
    */
    @RequestMapping("batchDelete")
    @ResponseBody
    public boolean batchDelete(@RequestBody Map<String,Object> map) {
        String ids = String.valueOf(map.get("ids"));
        List<Long> idsList = null;
        try {
        idsList = mapper.readValue(ids, new TypeReference<List<Long>>() {});
        } catch (IOException e) {
        e.printStackTrace();
        }
        return productService.deleteBatchIds(idsList);
    }
}

